<?php
include_once (PATH_SQL . 'user_management_sql.php');
include_once (PATH_MODEL . 'user_management_model.php');
include_once (PATH_VIEW_ADMIN . 'user_management_view.php');
$oModelUser = new Model_UserManagement();
$oViewUSer = new View_Admin_UserManangement($oModelUser);
$sAction = $_REQUEST['sAction'];
switch ($sAction) {
    case '':
        {
            $oViewUSer->displayManagerIndex($oModelUser);
            $oViewUSer->displayListUser($oModelUser);
            $oModelUser->unsetData('admin_status');
            break;
        }
    case 'update_status':
        {
            $oModelUser->unsetData('admin_id');
            $oModelUser->unsetData('admin_status');

            if (count($_POST['hotel_page_sort'] > 0)) {
                foreach ($_POST['hotel_page_sort'] as $sKey => $sValue) {
                    $oModelUser->setData('admin_id', $sKey);
                    $oModelUser->setData('admin_status', intval($_POST['admin_status'][$sKey]));
                    $oModelUser->updateUser();
                }
            }
            redir('?sMod=user_index');
            break;
        }
    case 'add_new_form':
        {
            $oViewUSer->displayNewUserIndex($oModelUser);
            break;
        }
    case 'do_add_new':
        {

            $password = md5($_POST['admin_password']);
            $oModelUser->setData('admin_password', $password);
            if ($_POST['admin_status'] == 'on') {
                $admin_status = 1;
            } else
                $admin_status = 0;
            $oModelUser->setData('admin_status', $admin_status);
            $oModelUser->setData("admin_template_id",$oModelUser->getData('template'));
            $oModelUser->setData("admin_category_id",$oModelUser->getData('category'));
            $result = $oModelUser->insertUser();
            if (!$result) {
                $sErrorMsg = $oModelUser->aLanguage['TEXT']['form_cannot_submit'] . $oModelUser->
                    aLanguage['TEXT']['alert_unique'];
                $oModelUser->setErrorMsg($sErrorMsg);
            } else {
                redir('?sMod=user_index');
            }
            break;
        }
    case 'do_delete':
        {
            $oModelUser->setData('admin_id', $oModelUser->getData('id'));
            $result_del = $oModelUser->deleteUser();

            if (!$result_del) {
                redir('?sMod=user_index&sSatus=delError');
                $sErrorMsg = $oModelUser->aLanguage['TEXT']['cannot_delete'] . $oModelUser->
                    aLanguage['TEXT']['alert_unique'];
                $oModelUser->setErrorMsg($sErrorMsg);
            } else {
                redir('?sMod=user_index');
            }
        }
    case 'edit_form':
        {
            $oModelUser->setData('admin_id', $oModelUser->getData('id'));
            $oViewUSer->displayEditUserIndex($oModelUser);
            $oViewUSer->displayInfoUser($oModelUser);
            break;
        }
    case 'do_edit':
        {
        	
            if ($_POST['admin_status'] == 'on') {
                $admin_status = 1;
            } else
                $admin_status = 0;
            $oModelUser->setData('admin_status', $admin_status);

            if ($_POST['admin_password'] == '') {
                $oModelUser->unsetData('admin_password');
            } 
			else 
			{
                $password = md5($_POST['admin_password']);
                $oModelUser->setData('admin_password', $password);
            }
            $oModelUser->setData('admin_id',$_POST['id']);
            
			$result_edit=$oModelUser->updateUser();
		
			if(!$result)
			{
				redir('?sMod=user_index&sSatus=updateError');
                $sErrorMsg = $oModelUser->aLanguage['TEXT']['cannot_delete'] . $oModelUser->
                aLanguage['TEXT']['alert_unique'];
                $oModelUser->setErrorMsg($sErrorMsg);
			}
			else
			{
				 redir('?sMod=user_index');
			}
            break;
        }
    case "setting_default":
        
        $oModelUser->setData('admin_id', $oModelUser->getData('id'));
        $userInfo = $oModelUser->getInfoUser();
        if($userInfo['admin_role']!=3){
            redir("?sMod=user_index");
        }
        $oViewUSer->displaySettingInfo($oModelUser, $userInfo);
        break;
    case "save_setting_default":
        $oModelUser->setData('admin_id', $oModelUser->getData('id'));
        $userInfo = $oModelUser->getInfoUser();
        $userInfo = $oModelUser->getInfoUser();
        if($userInfo['admin_role']!=3){
            redir("?sMod=user_index");
        }
        
        $oModelUser->setData("admin_template_id",$oModelUser->getData('template'));
        $oModelUser->setData("admin_category_id",$oModelUser->getData('category'));
        $oModelUser->updateUser();
        redir("?sMod=user_index");
}

$oViewUSer->displayPage($oModelUser);
